package com.yjd.pub.user.service;

import com.alibaba.fastjson.JSON;
import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.credit.model.CreditLineDataModel;
import com.yjd.comm.credit.service.ICreditLineDataService;
import com.yjd.comm.enums.CreditDataRefTableEnum;
import com.yjd.comm.enums.RecordIncr;
import com.yjd.comm.user.dto.ContactInfo;
import com.yjd.comm.user.model.UserDataModel;
import com.yjd.comm.user.dto.WorkInfo;
import com.yjd.comm.user.service.IUserDataService;
import com.yjd.comm.user.util.UserUtil;
import com.yjd.comm.util.ArithUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.pub.user.dao.IUserDataDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <b>description</b>：用户信息业务实现<br>
 * <b>time</b>：2017-06-27 08:59:19 <br>
 * <b>author</b>：  yangchangyan@yijiedai.com
 */
@Service("userDataService")
public class UserDataServiceImpl extends BaseServiceImpl implements IUserDataService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public UserDataModel insert(UserDataModel model) throws Exception {
		return this.userDataDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(UserDataModel model) throws Exception {
		return this.userDataDao.updateModel(model, true);
	}

	@Override
	public int updateByMap(Long user_id, Map<Object, Object> paramMap) throws Exception {
		paramMap.put("user_id", user_id);
		return this.userDataDao.updateByMap(paramMap, DbWREnums.WRITE.isWrite());
	}

	/**
	 * 根据id删除
	 * 
	 * @param user_id
	 * @return
	 * @throws Exception
	 */
	public int delete(long user_id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("user_id", user_id);
		return this.userDataDao.deleteModel(paramMap, true);
	}


	@Override
	public int updateByMap(Map<Object, Object> param) throws Exception {
		return this.userDataDao.updateByMap(param,true);
	}

	/**
	 * 用户信用额度增加
	 *
	 * @throws Exception
	 */
	@Override
	public void recoveryCredit(UserDataModel userDataModel, Long ref_id,Double loan_price,CreditDataRefTableEnum creditDataRefTableEnum, Long currtime) throws Exception {
		if(userDataModel==null||ref_id==null||creditDataRefTableEnum==null||currtime==null){
			FrameUtil.throwBaseException1("参数错误！");
		}
		if(userDataModel.getUser_id()==null){
			FrameUtil.throwBaseException1("用户不能为空！");
		}

		if(userDataModel.getVersion()==null){
			FrameUtil.throwBaseException1("当前版本号不能为空!");
		}

		if(userDataModel.getCredit_line()==null){
			FrameUtil.throwBaseException1("用户信用额度信息错误!");
		}

		Double residualAmount = ArithUtil.add(userDataModel.getResidual_amount(),loan_price);
		if(residualAmount > userDataModel.getCredit_line()){
			residualAmount = userDataModel.getCredit_line();
		}
		if(userDataDao.updateByMap(FrameUtil.newHashMap("user_id",userDataModel.getUser_id(),"where_version",
				userDataModel.getVersion(),"residual_amount",residualAmount),true)!=1){
			FrameUtil.throwBaseException1("系统繁忙，稍后重试。");
		}
		//记录用户信用额度使用明细
		CreditLineDataModel creditLineDataModel = new CreditLineDataModel();
		creditLineDataModel.setUser_id(userDataModel.getUser_id());
		creditLineDataModel.setCost(loan_price);
		creditLineDataModel.setType(RecordIncr.addAm.getCode());
		creditLineDataModel.setResidual_amount(residualAmount);
		creditLineDataModel.setRef_id(ref_id);
		creditLineDataModel.setRef_type(creditDataRefTableEnum.getValue());
		creditLineDataModel.setCreate_time(currtime);
		creditLineDataService.insert(creditLineDataModel);
	}

	@Override
	public int isUserInfoCompleted(long user_id) throws Exception {
		// 0：不完整，1：部分完整，2：完整
		int integrity = 0;
		UserDataModel userData = userDataDao.getModelById(user_id, true);
		if(null == userData){
			return integrity;
		}

		ContactInfo contactInfo = null;
		WorkInfo workInfo = null;
		try{
			contactInfo = JSON.parseObject(userData.getContacts(), ContactInfo.class);
			workInfo = JSON.parseObject(userData.getJob_info(), WorkInfo.class);
		}catch (Exception e){
			logger.error("user info format error:"+ e.getMessage());
			return integrity;
		}

		if(	UserUtil.isContactInfoCompleted(contactInfo) || UserUtil.isJobInfoCompleted(workInfo) ){
			integrity = 1;
		}

		if(	UserUtil.isContactInfoCompleted(contactInfo) && UserUtil.isJobInfoCompleted(workInfo) ){
			integrity = 2;
		}

		return integrity;
	}

	@Resource
	private IUserDataDao userDataDao;

	@Resource
	private ICreditLineDataService creditLineDataService;

	@Override
	public IBaseDao getBaseDao() {
		return this.userDataDao;
	}

}
